package dto

import (
	"time"
	"tuowei.com/app/admin/models"
	"tuowei.com/common/dto"
	common "tuowei.com/common/models"
)

// 线下订单详情
type SysOfflineOrderDetailRequest struct {
	Id int `json:"id" form:"id" search:"type:exact;column:id;table:sys_offline_order" binding:"required"`
}

// 在线订单详情
type SysOnlineOrderDetailRequest struct {
	Id int `json:"id" form:"id" search:"type:exact;column:id;table:sys_online_order" binding:"required"`
}

type SysDeliverGoodsRequest struct {
	Id             int    `json:"id" form:"id" search:"type:exact;column:id;table:sys_material_order" binding:"required"`
	ExpressNumber  string `json:"express_number" form:"express_number"`
	ExpressCompany string `json:"express_company" form:"express_company"`
	Comments       string `json:"comments" form:"comments"`
}

func (s *SysDeliverGoodsRequest) Generate(model *models.MaterialOrder) {
	model.Id = s.Id
	model.ExpressNumber = s.ExpressNumber
	model.ExpressCompany = s.ExpressCompany
	model.Comments = s.Comments
	model.Status = 4
	model.Material = []*models.SysMaterial{}
	model.SendAt = time.Now()
}

func (s *SysDeliverGoodsRequest) GetId() interface{} {
	return s.Id
}

type UpdateMaterialOrderRequest struct {
	OrderNumber string `json:"order_number" form:"order_number" search:"type:exact;column:order_number;table:sys_material_order" binding:"required"`
	Status      int    `json:"status" form:"status"`
	common.ControlBy
}

type DelOrderRequest struct {
	OrderNumber []string `json:"order_number" form:"order_number" search:"type:exact;column:order_number;table:sys_material_order" binding:"required"`
	common.ControlBy
}

func (s *UpdateMaterialOrderRequest) Generate(model *models.MaterialOrder) {
	model.OrderNumber = s.OrderNumber
	model.Status = s.Status
	model.Material = []*models.SysMaterial{}
	if s.Status == 3 { //付款
		model.PayAt = time.Now().Local().Format("2006-01-02T15:04:05.000+08:00")
	}

}

func (s *UpdateMaterialOrderRequest) GetOrderNumber() interface{} {
	return s.OrderNumber
}

type UpdateOnlineOrderRequest struct {
	OrderNumber string `json:"order_number" form:"order_number" search:"type:exact;column:order_number;table:sys_online_order" binding:"required"`
	Status      int    `json:"status" form:"status"`
	common.ControlBy
}

func (s *UpdateOnlineOrderRequest) Generate(model *models.OnlineOrder) {
	model.OrderNumber = s.OrderNumber
	model.Status = s.Status
	model.Course = []*models.SysCourse{}
	if s.Status == 2 { //付款
		model.Status = 5 //完成
		model.PayAt = time.Now().Local().Format("2006-01-02T15:04:05.000+08:00")
	}

}

func (s *UpdateOnlineOrderRequest) GetOrderNumber() interface{} {
	return s.OrderNumber
}

// 资料订单详情
type SysMaterialOrderDetailRequest struct {
	Id int `json:"id" form:"id" search:"type:exact;column:id;table:sys_material_order" binding:"required"`
}

func (req *SysOnlineOrderDetailRequest) GetNeedSearch() interface{} {
	return *req
}

type SysMaterialOrderAddRequest struct {
	BuyerId        int     `comment:"买家id" json:"buyer_id"`
	Price          float64 `comment:"订单价格" json:"price"`
	Commission     float64 `comment:"佣金" json:"commission"`
	Postage        float64 `comment:"邮费" json:"postage"`
	MaterialId     []int   `comment:"资料id" json:"material_id"`
	ShoppingCartId []int   `comment:"购物车id" json:"shopping_cart_id"`
	Address        string  `comment:"收获地址" json:"address"`
	PayMethod      int     `comment:"支付方式" json:"pay_method"`
	common.ControlBy
}

func (s *SysMaterialOrderAddRequest) Generate(model *models.MaterialOrder) {
	//model.CourseId = s.CourseId
	model.OrderNumber = GenerateNum(time.Now())
	model.BuyerId = s.BuyerId
	model.Price = s.Price
	model.Commission = s.Commission
	model.Postage = s.Postage
	model.PayMethod = s.PayMethod
	model.Status = 1
	model.Address = s.Address
	//本次订单付款成功获得积分
	model.TheScore = s.Price / 100
	for _, m := range s.MaterialId {
		s := &models.SysMaterial{}
		s.Id = m
		model.Material = append(model.Material, s)
	}
}

type SysLinkOrderAddRequest struct {
	OrderType   string `gorm:"type:varchar(64);default:'';comment:关联的订单类型" json:"order_type"`
	OrderId     int    `gorm:"type:int(10);default:0;index;comment:关联的订单id" json:"order_id"`
	Status      int    `gorm:"type:tinyint(2);not null;default:1;comment:订单状态1->未付款，2-已付款，3-招待发货，4-待收货，5-完成" json:"status"`
	BuyerId     int    `gorm:"type:int(10);not null;default:0;index;comment:买家id" json:"buyer_id"`
	OrderNumber string `gorm:"type:varchar(64);not null;default:'';index;comment:订单编号" json:"order_number"`
	common.ControlBy
}

func (s *SysLinkOrderAddRequest) Generate(model *models.LinkOrder) {
	model.OrderType = s.OrderType
	model.OrderId = s.OrderId
	model.Status = s.Status
	model.BuyerId = s.BuyerId
	model.OrderNumber = s.OrderNumber

}

type SysOnlineOrderAddRequest struct {
	BuyerId        int     `comment:"买家id" json:"buyer_id"`
	TeacherId      int     `comment:"教师id" json:"teacher_id"`
	Price          float64 `comment:"订单价格" json:"price"`
	Commission     float64 `comment:"佣金" json:"commission"`
	CourseId       []int   `comment:"在线课程id" json:"course_id"`
	ShoppingCartId []int   `comment:"购物车id" json:"shopping_cart_id"`
	PayMethod      int     `comment:"支付方式" json:"pay_method"`
	CourseType     string  `json:"course_type" gorm:"type:varchar(50);comment:课程类型，online，live"` //课程类型
	ShareId        int     `gorm:"type:int(10);not null;default 0;comment:分享者学生id" json:"share_id"`
	IsBindOffline  int     `json:"is_bind_offline" gorm:"tinyint(1);default:0;comment:是否关联面授课程"`
	common.ControlBy
}

func (s *SysOnlineOrderAddRequest) Generate(model *models.OnlineOrder) string {
	//model.CourseId = s.CourseId
	model.OrderNumber = GenerateNum(time.Now())
	model.BuyerId = s.BuyerId
	model.TeacherId = s.TeacherId
	model.Price = s.Price
	model.Commission = s.Commission
	model.PayMethod = s.PayMethod
	model.Status = 1
	model.CourseType = s.CourseType
	model.ShareId = s.ShareId
	if s.IsBindOffline != 0 {
		model.IsBindOffline = s.IsBindOffline
	}
	//本次订单付款完成获取的积分
	model.TheScore = s.Price / 100
	for _, m := range s.CourseId {
		s := &models.SysCourse{}
		s.CourseId = m
		model.Course = append(model.Course, s)
	}
	return model.OrderNumber
}

type SysOfflineOrderAddRequest struct {
	BuyerId    int     `comment:"买家id" json:"buyer_id"`
	Price      float64 `comment:"订单价格" json:"price"`
	Commission float64 `comment:"佣金" json:"commission"`
	CourseId   []int   `comment:"线下课程id" json:"course_id"`
	Name       string  `gorm:"type:varchar(50);default:'';comment:买家姓名" json:"name"`
	Phone      string  `gorm:"type:varchar(20);default:'';comment:买家电话" json:"phone"`
	//TeachingPointId int     `gorm:"type:int(10);default:0;comment:教学点" json:"teaching_point_id"`
	CollegeId int    `comment:"院校id" json:"college_id"`
	Grade     string `gorm:"type:varchar(200);default:'';comment:年级" json:"grade"`
	KeBie     string `gorm:"type:varchar(200);default:'';comment:科别" json:"ke_bie"`
	OrderType string `gorm:"-" json:"order_type"`
	common.ControlBy
}

func (s *SysOfflineOrderAddRequest) Generate(model *models.OfflineOrder) {
	//model.CourseId = s.CourseId
	model.OrderNumber = GenerateNum(time.Now())
	model.BuyerId = s.BuyerId
	model.Price = s.Price
	model.Commission = s.Commission
	model.PayMethod = int(models.CashPayMethod)
	for _, m := range s.CourseId {
		s := &models.SysOfflineCourse{}
		s.CourseId = m
		model.Course = append(model.Course, s)
	}
	model.Name = s.Name
	model.Phone = s.Phone
	model.CollegeId = s.CollegeId
	model.Grade = s.Grade
	model.KeBie = s.KeBie
	now := time.Now()
	model.Year = now.Year()
	model.Month = int(now.Month())
	//model.TeachingPointId = s.TeachingPointId
}

// 资料订单请求列表
type SysMaterialOrderListRequest struct {
	dto.Pagination `search:"-"`
	OrderNumber    int    `json:"order_number" form:"order_number"  search:"type:exact;column:order_number;table:sys_material_order" comment:"状态"` // 状态
	Status         int    `json:"status" form:"status"  search:"type:exact;column:status;table:sys_material_order" comment:"状态"`                   // 状态
	StartTime      string `json:"beginTime" form:"beginTime" search:"type:gte;column:created_at;table:sys_material_order"`
	EndTime        string `json:"endTime" form:"endTime" search:"type:lte;column:created_at;table:sys_material_order"`
	Province       string `json:"province" form:"province" search:"type:exact;column:province;table:sys_material_order" comment:"大区"`
	BuyerId        int    `json:"buyer_id" form:"buyer_id"  search:"type:exact;column:buyer_id;table:sys_material_order" comment:"买家id"` // 买家id
	StartSendTime  string `json:"start_send_time" form:"start_send_time" search:"type:gte;column:send_at;table:sys_material_order"`
	EndSendTime    string `json:"end_send_time" form:"end_send_time" search:"type:lte;column:send_at;table:sys_material_order"`
	Phone          string `json:"phone" form:"phone" search:"-"`
	DeliverId      int    `json:"deliver_id" form:"deliver_id" search:"type:exact;column:deliver_id;table:sys_material_order"`
}

// 在线订单请求列表
type SysOnlineOrderListRequest struct {
	dto.Pagination `search:"-"`
	OrderNumber    string `json:"order_number" form:"order_number"  search:"type:exact;column:order_number;table:sys_online_order" comment:"状态"` // 状态
	Status         int    `json:"status" form:"status"  search:"type:exact;column:status;table:sys_online_order" comment:"状态"`                   // 状态
	StartTime      string `json:"beginTime" form:"beginTime" search:"type:gte;column:created_at;table:sys_online_order"`
	EndTime        string `json:"endTime" form:"endTime" search:"type:lte;column:created_at;table:sys_online_order"`
	Province       string `json:"province" form:"province" search:"type:exact;column:province;table:sys_online_order" comment:"大区"`
	BuyerId        int    `json:"buyer_id" form:"buyer_id"  search:"type:exact;column:buyer_id;table:sys_online_order" comment:"买家id"` // 买家id
	Phone          string `json:"phone" form:"phone" search:"-"`
}

// 在线订单请求列表
type SysLinkOrderListRequest struct {
	dto.Pagination `search:"-"`
	Status         int `json:"status" form:"status"  search:"type:exact;column:status;table:sys_link_order" comment:"状态"`         // 状态
	BuyerId        int `json:"buyer_id" form:"buyer_id"  search:"type:exact;column:buyer_id;table:sys_link_order" comment:"买家id"` // 买家id
}

func (req *SysLinkOrderListRequest) GetNeedSearch() interface{} {
	return *req
}

// 在线订单请求列表
type SysOfflineOrderListRequest struct {
	dto.Pagination `search:"-"`
	OrderNumber    int    `json:"order_number" form:"order_number"  search:"type:exact;column:order_number;table:sys_offline_order" comment:"状态"` // 状态
	Status         int    `json:"status" form:"status"  search:"type:exact;column:status;table:sys_offline_order" comment:"状态"`                   // 状态
	StartTime      string `json:"beginTime" form:"beginTime" search:"type:gte;column:created_at;table:sys_offline_order"`
	EndTime        string `json:"endTime" form:"endTime" search:"type:lte;column:created_at;table:sys_offline_order"`
	Province       string `json:"province" form:"province" search:"type:exact;column:province;table:sys_offline_order" comment:"大区"`
	Phone          string `json:"phone" form:"phone" search:"-"`
	CourseName     string `json:"course_name" form:"course_name" search:"-"`                                                                  //线下课程名字
	RecheckUserId  int    `json:"recheck_user_id" form:"recheck_user_id" search:"type:exact;column:recheck_user_id;table:sys_offline_order" ` // 复审人
}

// 线下订单详情
type SysOfflineOrderRecheckUpdateRequest struct {
	Id         int    `json:"id" comment:"主键id"`           // 主键id
	RecheckOk  int    `json:"recheck_ok" comment:"复审是否通过"` // 复审是否通过,1通过，2不通过
	FollowUp   string `json:"follow_up" comment:"跟进意见"`    // 跟进意见
	ContractNo string `json:"contract_no" comment:"合同编号"`  // 合同编号
	Recheck    string `json:"recheck" comment:"复审意见"`      // 复审意见
}

func (s *SysOfflineOrderRecheckUpdateRequest) Generate(model *models.OfflineOrder) {
	if s.Id != 0 {
		model.Id = s.Id
	}
	if s.FollowUp != "" {
		model.FollowUp = s.FollowUp
	}
	if s.ContractNo != "" {
		model.ContractNo = s.ContractNo
	}
	if s.Recheck != "" {
		model.Recheck = s.Recheck
	}
	model.Course = []*models.SysOfflineCourse{}
	//复审通过
	if s.RecheckOk == 1 {
		//订单状态1->提交，2-待跟进，3-财务复审，4-完成
		model.Status = 4
		model.CompletePay = 1
		model.PayAt = time.Now().Local().Format("2006-01-02T15:04:05.000+08:00")
	} else {
		model.Status = 2
	}

}

func (req *SysOnlineOrderListRequest) ParseTime() error {
	if req.StartTime != "" {
		_, err := time.Parse("2006-01-02 15:04:05", req.StartTime)
		if err != nil {
			return err
		}
	}
	if req.EndTime != "" {
		_, err := time.Parse("2006-01-02 15:04:05", req.EndTime)
		if err != nil {
			return err
		}
	}
	return nil
}

func (req *SysOnlineOrderListRequest) GetNeedSearch() interface{} {
	return *req
}

type OrderCountRequest struct {
	OrderCountList []OrderCount `json:"order_count_list"`
}
type OrderCount struct {
	Good
}
type BuyNowRequest struct {
	Goods   []Good `json:"goods" form:"goods"`
	BuyerId int    `comment:"买家id" json:"buyer_id"`
}
type Good struct {
	Id int `json:"id" form:"id" binding:"required"`
	//商品类型OnlineOrder，MaterialOrder,LiveOrder
	OrderType string `json:"order_type" form:"order_type"`
	Num       int    `json:"num" form:"num"`
}

type AssignOrderRequest struct {
	Id                  int `json:"id" binding:"required" `                   // 订单id
	EnrollmentTeacherId int `json:"enrollment_teacher_id" binding:"required"` //招生老师id
}
